import { Injectable } from '@angular/core';
import { concat, Observable, zip } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { OrganizationService,RoleService } from 'sparrow-permission-lib';
import { FlatNode } from '../domain/flat-node';
import { NzTreeGetChildren } from './nz-tree-get-children';

@Injectable({
  providedIn: 'root'
})
export class NzTreeGetRoleChildrenService implements NzTreeGetChildren{

  constructor(private roleService: RoleService, private orgService: OrganizationService) { }
  getChildren(node: FlatNode): Observable<FlatNode[]> {
    const roles$: any = this.orgService
    .getChildren1(node.id, 'ROLE', { page: 0, size: 50 })
    .pipe(
      // tap((res) => console.log(res)),
      map((res: any) => res.content),
      switchMap((orgRoles: any) =>
        zip(
          ...orgRoles.map((orgRole: any) =>
            this.roleService.get3(orgRole.id.roleId).pipe(
              map((role: any) => {
                return {
                  id: role.id,
                  label: role.name,
                  level: node.level + 1,
                  expandable: true,
                  type: 'ROLE',
                  childCount: 0,
                  orgId: orgRole.id.organizationId,
                };
              }),
              switchMap((roleNode) =>
                this.roleService.getChildren(roleNode.orgId + '_' + roleNode.id).pipe(
                  map((count) => {
                    roleNode.expandable = count.length! > 0 ? true : false;
                    roleNode.childCount = count.length;
                    return roleNode;
                  })
                )
              )
            )
          )
        )
      ),
      // tap((result) => console.log('2222======', result))
    );

  const orgRole$ = this.roleService.get3(node.id).pipe(
    switchMap((role: any) =>
      this.roleService.getChildren(node.orgId + '_' + node.id).pipe(
        switchMap((parents) =>
          zip(
            ...parents.map((parent: any) =>
              this.roleService.get3(parent.id.id.roleId).pipe(
                map((role: any) => {
                  return {
                    id: role.id,
                    label: role.name,
                    level: node.level + 1,
                    expandable: false,
                    type: 'ROLE',
                    childCount: 0,
                    orgId: parent.id.id.organizationId,
                  };
                }),
                switchMap((roleNode) =>
                  this.roleService
                    .getChildren(roleNode.orgId + '_' + roleNode.id)
                    .pipe(
                      map((count) => {
                        roleNode.expandable = count.length! > 0 ? true : false;
                        roleNode.childCount = count.length;
                        return roleNode;
                      })
                    )
                )
              )
            )
          )
        )
      )
    )
  );

  const orgs$: any = this.orgService
    .getChildren1(node.id, 'ORGANIZATION', { page: 0, size: 50 })
    .pipe(
      map((res) => res.content),
      // tap((res) => console.log(res)),
      switchMap((orgs: any) =>
        zip(
          ...orgs.map((org: any) =>
            this.orgService
              .get4(
                org.id.organizationId === undefined
                  ? org.id
                  : org.id.organizationId
              )
              .pipe(
                map((org_: any) => {
                  return {
                    id: org_.id,
                    label: org_.name,
                    level: node.level + 1,
                    expandable: true,
                    type: 'ORG',
                    childCount: 0,
                  };
                }),
                switchMap((orgNode) =>
                  this.orgService
                    .getChildren1(orgNode.id, 'ORGANIZATION', {
                      page: 0,
                      size: 1,
                    })
                    .pipe(
                      map((count) => {
                        orgNode.expandable =
                          count.totalElements! > 0 ? true : false;
                        orgNode.childCount = count.totalElements!;
                        return orgNode;
                      })
                    )
                )
              )
          )
        )
      ),
      // tap((result) => console.log('2222======', result))
    );

  const child$: any = concat(orgs$, roles$);
  return node.type === 'ROLE' ? orgRole$ : child$;
  }
}
